﻿using System;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Portal.Modules.CMS.BO;
using System.Collections.Generic;
using BIS.Portal.Core.Global;
using BIS.Utility.Converter;

namespace BIS.Portal.Modules.CMS
{
    public partial class CmsArticleListDisplay : ControlBase
    {
        #region Lấy dữ liệu settings
        public string SLTinHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsArticleListDisplay.txtSoLuongTin, PortalContext.LanguageAlias); }
        }
        public bool HienThiAnh
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleListDisplay.imgAnh, PortalContext.LanguageAlias); }
        }
        public bool HienThiTieuDeTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleListDisplay.ddlTieuDeTin, PortalContext.LanguageAlias); }
        }
        public bool HienThiMoTaTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleListDisplay.ddlMotTaTin, PortalContext.LanguageAlias); }
        }
        public bool HienThiLinkCTTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleListDisplay.ddlLinkXemCT, PortalContext.LanguageAlias); }
        }
        public bool HienThiTinLienQuan
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleListDisplay.ddlTinLienQuan, PortalContext.LanguageAlias); }
        }
        public string SLTinLienQuan
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsArticleListDisplay.txtSLTinLienQuan, PortalContext.LanguageAlias); }
        }
        #endregion
        private string GetCateName(int iCateId)
        {
            return ClientCmsArticleCategoryAndCategoryBO.GetCategoryNameById(iCateId).Category_name;
        }
        private int cateId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            loadData();
        }
        public void loadData()
        {
            if (Request[ModuleQueryStringKey.CategoryId] != null)
            {
                cateId = Functions.GetInt(Request[ModuleQueryStringKey.CategoryId]);
                IList<ClientCmsArticleAndCategoryEntity> list = null;
                if (Request[ModuleQueryStringKey.PageIndex] != null)
                {
                    int page_index = Functions.GetInt(Request[ModuleQueryStringKey.PageIndex]);
                    list =
                        ClientCmsArticleBO.GetArticleByCategoryId(cateId, PortalContext.LanguageInUseId,
                                                                        page_index - 1, Functions.GetInt(SLTinHienThi));
                }
                else
                {
                    list =
                          ClientCmsArticleBO.GetArticleByCategoryId(cateId, PortalContext.LanguageInUseId, 0,
                                                                          Functions.GetInt(SLTinHienThi));
                }


                Tin_Tuc.DataSource = list;
                Tin_Tuc.DataBind();
                Nhom_Tin.Text = GetCateName(cateId);
                if (list.Count > 0)
                {
                PagingControl.QueryStringName = ModuleQueryStringKey.PageIndex;
                PagingControl.UrlRewriteRule = ModuleQueryStringKey.UrlPaging;
                PagingControl.TotalRecord = ClientCmsArticleBO.ArticleCount;
                PagingControl.RecordPerPage = Functions.GetInt(SLTinHienThi);
                PagingControl.RefreshPaging();
                }
            }
        }

        protected void rptTinTuc_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                #region Url Chi Tiết
                string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                    new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                    new string[]
                                            {
                                           Convert.ToString(objArticle.Category_id),
                                           Convert.ToString(objArticle.Article_id),
                                           Functions.UnicodeToKoDauAndGach(GetCateName(objArticle.Category_id)),
                                           Functions.UnicodeToKoDauAndGach(objArticle.Article_title)
                                           }
                );

                #endregion
                
                #region Bind dữ liệu Hyperlink Tiêu đề
                if (HienThiTieuDeTin)
                {
                    HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
                }
                #endregion

                #region Bind dữ liêu Image
                if (HienThiAnh)
                {
                    Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                    if (imgAnh != null)
                    {
                        imgAnh.ImageUrl = objArticle.Thumb_image;
                        imgAnh.AlternateText = objArticle.Article_title;
                        imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                    }
                }


                #endregion

                #region Bind Dư liệu nội dung
                if (HienThiMoTaTin)
                {
                    Literal ltrNoiDung = e.Item.FindControl("Noi_Dung") as Literal;
                    if (ltrNoiDung != null) ltrNoiDung.Text = objArticle.Article_description;
                }
                #endregion

                #region Bind Dữ liệu Link Chi Tiết
                if (HienThiLinkCTTin)
                {
                    HyperLink lnkLinkCT = e.Item.FindControl("Link_Xem_Tiep") as HyperLink;
                    if (lnkLinkCT != null)
                    {
                        lnkLinkCT.Text = GetLanguageValue("ReadMoreButton");
                        lnkLinkCT.NavigateUrl = urlChiTiet;
                    }
                }
                #endregion

                #region Bind Tin Liên Quan
                if (HienThiTinLienQuan)
                {
                    IList<ClientCmsArticleAndCategoryEntity> list =
                        ClientCmsArticleBO.GetArticleDisplayRelated(
                            cateId, PortalContext.LanguageInUseId, Functions.GetInt(SLTinLienQuan), objArticle.Article_id);
                    Repeater rptTinLienQuan = e.Item.FindControl("Tin_Lien_Quan") as Repeater;
                    
                    if (rptTinLienQuan != null)
                    {
                        rptTinLienQuan.ItemDataBound += new RepeaterItemEventHandler(rptTinLienQuan_ItemDataBound);
                        rptTinLienQuan.DataSource = list;
                        rptTinLienQuan.DataBind();
                    }
                }
                #endregion
            }
        }

        private void rptTinLienQuan_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                #region Url Chi Tiết
                string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                    new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                    new string[]
                                            {
                                           Convert.ToString(objArticle.Category_id),
                                           Convert.ToString(objArticle.Article_id),
                                           Functions.UnicodeToKoDau(GetCateName(objArticle.Category_id)),
                                           Functions.UnicodeToKoDau(objArticle.Article_title)
                                           }
                );

                #endregion

                HyperLink lnkTieuDeTinLienQuan = e.Item.FindControl("Tieu_De_Tin_Lien_Quan") as HyperLink;
                if (lnkTieuDeTinLienQuan != null)
                {
                    lnkTieuDeTinLienQuan.NavigateUrl = urlChiTiet;
                    lnkTieuDeTinLienQuan.Text = objArticle.Article_title;
                }
            }
        }

    }
}